import { getUserInfo } from "@/commonApi/login-api";
import {message} from 'ant-design-vue'
export default {
  state: {
    token: sessionStorage.getItem('token'),
    user: null
  },
  mutations: {
    setToken(state, value) {
      state.token = value
      sessionStorage.setItem("token", value)
    },
    setUser(state, value) {
      state.user = value
    },
    removeToken(state){
      state.token = null
      sessionStorage.removeItem('token')
    },
    removeUser(state){
      state.user = null
      sessionStorage.removeItem('menuItem')
      sessionStorage.removeItem('menuSub')
    }
  },
  actions: {
    addUser({ commit }, token) {
      return new Promise((reslove, reject) => {
        getUserInfo(token).then(data => {
          if (data.ok) {
            commit('setUser', data.data)
            reslove(true)
          } else {
            message.error(data.msg||'服务器开小差了');
            reject(false)
          }
        }).catch(e => {
          reject(false)
        })
      })

    },
    removeToken({commit}){
       commit('removeToken')
    }
    ,
    addToken({ commit }, token) {
      commit('setToken', token)
    },
    removeUser({ commit }){
      commit('removeUser')
    }
  },
  getters: {
    getToken(state) {
      return state.token;
    },
    getUserInfo(state) {
      return state.user;
    }
  }
}